<?php
/**
 * 首页
 * User: yuanxuncheng
 * File: Index.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page;

use app\api\model\dao\Ad;
use app\api\model\dao\Banner;
use app\api\model\dao\ComicfansExperience;
use app\api\model\dao\FlashSale;
use app\api\model\dao\Goods;
use app\api\model\dao\GoodsCategory;
use app\api\model\dao\Nav;
use app\api\model\dao\Note;
use app\api\model\dao\PromotionRegion;
use app\api\model\dao\PromotionRegionGoods;
use app\api\model\dao\UserAccount;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class Home extends PageBase
{

    private $objNav;
    private $objBanner;
    private $objGoods;
    private $objGoodsCategory;
    private $objAd;
    private $objFlashSale;
    private $objPromotionRegion;
    private $objPromotionRegionGoods;
    private $objComicfansExperience;
    private $objUserAccount;
    private $objNote;

    public function __construct()
    {
        parent::__construct();
        $this->objNav = new Nav();
        $this->objBanner = new Banner();
        $this->objGoods = new Goods();
        $this->objGoodsCategory = new GoodsCategory();
        $this->objAd = new Ad();
        $this->objFlashSale = new FlashSale();
        $this->objPromotionRegion = new PromotionRegion();
        $this->objPromotionRegionGoods = new PromotionRegionGoods();
        $this->objComicfansExperience = new ComicfansExperience();
        $this->objNote = new Note();
        $this->objUserAccount = new UserAccount();
    }

    public function excute($param)
    {

        $return = array();
        $now = time();
        // 获取顶部轮播切换
        $arrFields = [
            'ad_id',
            'ad_name',
            'ad_link',
            'ad_code',
            'ad_type',
            'ad_value',
        ];
        $arrConds = [
            'pid' => ['=', 1],
            'enabled' => ['=', 1],
            'status' => ['=', 0],
            //'start_time' => ['<=', $now],
            //'end_time' => ['>=', $now],
        ];
        $ads = $this->objAd->getAdByCond($arrConds, $arrFields);
        $return['ad'] = !empty($ads) ? $ads : array();

        // 获取导航
        $navs = $this->objNav->getNav();
        $return['nav'] = !empty($navs) ? $navs : array();


        // 限时抢购
        $now = time();
        $start = strtotime(date('Y-m-d', $now));
        $end = $start + (24 * 60 * 60);
        $arrConds = [
            'is_end' => ['=', 0],
            'start_time' => ['<=', $end],
            'end_time' => ['>=', $start],
        ];
        $arrFields = ['goods_id', 'start_time', 'end_time', 'is_end'];
        $flashSales = $this->objFlashSale->geFlashSaleByCond($arrConds, $arrFields, 0, 2);
        $goods_arr = array();
        if (!empty($flashSales)) {
            $goods_ids = array_map(function ($v) {
                return $v['goods_id'];
            }, $flashSales);
            $goods = $this->objGoods->getGoods(implode(',', $goods_ids));
            foreach ($goods as $good) {
                $goods_arr[$good['goods_id']] = $good;
            }
            foreach ($flashSales as &$flashSale) {
                if (isset($goods_arr[$flashSale['goods_id']])) {
                    $flashSale['start_time'] = date('Y-m-d H:m:s', $flashSale['start_time']);
                    //    $flashSale['end_time'] = date('Y-m-d H:m:s', $flashSale['end_time']);
                    $start = strtotime(date('Ymd'));
                    $flashSale['end_time'] = date('Y-m-d H:m:s', $start + (24 * 60 * 60));
                    $flashSale['market_price'] = $goods_arr[$flashSale['goods_id']]['market_price'];
                    $flashSale['shop_price'] = $goods_arr[$flashSale['goods_id']]['shop_price'];
                    $flashSale['price'] = $goods_arr[$flashSale['goods_id']]['shop_price'];
                    // 加上折扣
                    $flashSale['discount'] = round($goods_arr[$flashSale['goods_id']]['shop_price'] / $goods_arr[$flashSale['goods_id']]['market_price'], 2) * 10;
                    $flashSale['goods_thumb'] = $goods_arr[$flashSale['goods_id']]['goods_thumb'];
                }
            }
        }

        $return['flash_sale'] = !empty($flashSales) ? $flashSales : array();
        //$return['flash_sale']['list'] = !empty($flashSales) ? $flashSales : array();
        //$return['flash_sale']['now'] = date('Y-m-d H:m:s', $now);
        //$return['flash_sale']['end'] = date('Y-m-d H:m:s', strtotime(date('Y-m-d', $now))+(60*60*24));

        // 爆品预订
        $arrConds = array(
            'parent_id' => 22,
        );
        $subPromotions = $this->objPromotionRegion->getPromotionRegionByConds($arrConds);
        $return['middle_region'] = $subPromotions;


        // 获取漫友体验
        $note_ids = [-1];
        $user_ids = [-1];
        $user = [];
        $experice = $this->objComicfansExperience->getComicfansExperienceByCond(['status' => ['=', 0]], [], 0, 6);
        $note_ids = array_map(function ($v) {
            return $v['note_id'];
        }, $experice);
        $notes = $this->objNote->getNoteByids(implode(',', $note_ids), ['id', 'creator']);
        $user_ids = array_map(function ($v) {
            return $v['creator'];
        }, $notes);
        if (!empty($user_ids)) {
            $userinfo = $this->objUserAccount->getUserAccountByUserids($user_ids, ['id', 'user_name', 'avatar']);
            foreach ($userinfo as $u) {
                $user[$u['id']] = $u;
            }
            foreach ($notes as &$note) {
                $note['note_id'] = $note['id'];
                $note['user_id'] = $note['creator'];
                $note['user_name'] = $user[$note['creator']]['user_name'];
                $note['avatar'] = $user[$note['creator']]['avatar'];
                unset($note['creator']);
                unset($note['id']);
            }
        }

        $return['experices'] = !empty($notes) ? $notes : array();

        // 活动专区
        $arrConds = array(
            'rid' => ['in', [6, 27]],
        );
        $promotions = $this->objPromotionRegion->getPromotionRegionByConds($arrConds);
        if (!empty($promotions)) {
            foreach ($promotions as &$promotion) {
                $subPromotions = $this->objPromotionRegion->getPromotionRegionByConds(array('parent_id' => $promotion['rid']));
                $promotion['subPromotions'] = $subPromotions;
            }
        }
        $return['promotions'] = !empty($promotions) ? $promotions : array();

        $this->data = $return;
        return $this->getRes();
    }
}
